#include "Sprite.h"

/*
	Sprite
*/

Sprite::Sprite()
{

}

Sprite::~Sprite()
{
	ReleaseSprites();
}

void Sprite::ReleaseSprites()
{
	SAFE_RELEASE( mSprite );
}

void Sprite::OnInit()
{
	HR( D3DXCreateSprite( RenderFrame::GetD3DDevice(), &mSprite ) );
}

void Sprite::Render()
{
	HR( RenderFrame::GetD3DDevice()->BeginScene() );
	HR( mSprite->Begin( D3DXSPRITE_OBJECTSPACE | D3DXSPRITE_ALPHABLEND ) );

	for ( int i = 0; i != mEntities.size(); ++i ) {
		if ( !mEntities[ i ]->IsVisible() )
			continue;

		HR( mSprite->SetTransform( &mEntities[ i ]->GetTransform() ) );
		RECT* texCoord		= NULL;
		D3DXVECTOR3 center	= mEntities[ i ]->GetCenter();
		if ( mEntities[ i ]->IsSubTexture() ) {
			texCoord = &( mEntities[ i ]->GetTexCoord() );
			center	 = D3DXVECTOR3( texCoord->right - texCoord->left, texCoord->bottom - texCoord->top, 0 );
			center *= 0.5f;
		}

		HR( mSprite->Draw( mEntities[ i ]->GetTexture(), 
						   texCoord, 
						   &center, 
						   NULL, 
						   D3DCOLOR_XRGB( 255, 255, 255 ) ) );
	}

	HR( mSprite->Flush() );
	HR( mSprite->End() );

	HR( RenderFrame::GetD3DDevice()->EndScene() );
}

int Sprite::AddSprite( Tgo2DEntity* entity )
{
	int idx = mEntities.size();
	mEntities.push_back( entity );
	return idx;
}

std::pair< int, int > Sprite::AddSpritesList( vector< Tgo2DEntity* > entities ) 
{
	std::pair< int, int > interval;
	interval.first = mEntities.size() > 0? mEntities.size(): 0;
	interval.second = interval.first + entities.size();
	for ( int i = 0; i != entities.size(); ++i ) {
		mEntities.push_back( entities[ i ] );
	}
	if ( interval.second == interval.first ) {
		return std::pair<int, int>( 0, 0 );
	}
	return interval;
}

void Sprite::RemoveSprite( int i )
{
	mEntities.erase( mEntities.begin() + i );
}

void Sprite::RemoveSpriteList( int i, int j)
{
	if ( j <= 0 ) return;
	mEntities.erase( mEntities.begin() + i, mEntities.begin() + j );
}

Tgo2DEntity* Sprite::GetSprite( int i )
{
	return mEntities[ i ];
}

/*
	Tgo2DEntity
*/

Tgo2DEntity::Tgo2DEntity( D3DXVECTOR3 pos, float angle ):mPos( pos ),
														 mAngle( angle ), 
														 mIsSubTexture( false ), 
														 mIsVisible( true )
{
	
}

Tgo2DEntity::~Tgo2DEntity()
{
	mTex = NULL;
}

D3DXMATRIX Tgo2DEntity::GetTransform()
{
	D3DXMATRIX translate, rotation, transform;
	D3DXMatrixIdentity( &translate );
	D3DXMatrixIdentity( &rotation );
	D3DXMatrixIdentity( &transform );

	D3DXMatrixTranslation( &translate, mPos.x, mPos.y, mPos.z );
	D3DXMatrixRotationZ( &rotation, mAngle );
	D3DXMatrixMultiply( &transform, &rotation, &translate );

	transform._22 = -transform._22;
	return transform;
}

LPDIRECT3DTEXTURE9 Tgo2DEntity::GetTexture()
{
	return mTex->GetTexture();
}

RECT Tgo2DEntity::GetTexCoord()
{
	return mTexCoord;
}

D3DXVECTOR3 Tgo2DEntity::GetPos()
{
	return mPos;
}

float Tgo2DEntity::GetAngle()
{
	return mAngle;
}

D3DXVECTOR3 Tgo2DEntity::GetCenter()
{
	return mCenter;
}

UINT Tgo2DEntity::GetWidth()
{
	return mTex->GetWidth();
}
UINT Tgo2DEntity::GetHeight()
{
	return mTex->GetHeight();
}

bool Tgo2DEntity::IsVisible()
{
	return mIsVisible;
}

bool Tgo2DEntity::IsSubTexture()
{
	return mIsSubTexture;
}

void Tgo2DEntity::SetPos( D3DXVECTOR3 pos )
{
	mPos = pos;
}

void Tgo2DEntity::SetPos( D3DXVECTOR2 pos )
{
	mPos = D3DXVECTOR3( pos.x, pos.y, 0 );
}

void Tgo2DEntity::SetCenter( D3DXVECTOR3 center )
{
	mCenter = center;
}

void Tgo2DEntity::SetAngle( float angle )
{
	mAngle = angle;
}

void Tgo2DEntity::SetTexture( string texKey )
{
	mTex = TextureMgr::Instance()->GetTexture( texKey );
	SetCenter( D3DXVECTOR3( mTex->GetWidth() * 0.5f, mTex->GetHeight() * 0.5f, 0 ) );
}

void Tgo2DEntity::SetTexCoord( RECT texCoord )
{
	mTexCoord = texCoord;
	mIsSubTexture = true;
}

void Tgo2DEntity::SetState( bool visible )
{
	mIsVisible = visible;
}


